package gold.digger;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC290 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public boolean wordPattern(String pattern, String s) {
            String[] arr = s.split(" ");
            if (arr.length != pattern.length()) return false;

            Map<Character, String> map = new HashMap<Character, String>();
            Map<String, Character> valMap = new HashMap<>();
            for (int i = 0; i < arr.length; i++) {
                char curKey = pattern.charAt(i);
                String curVal = arr[i];

                if (map.containsKey(curKey) && !map.get(curKey).equals(curVal)) return false;
                if (valMap.containsKey(curVal) && valMap.get(curVal).charValue() != curKey) return false;

                if (!map.containsKey(curKey)) map.put(curKey, curVal);
                if (!valMap.containsKey(curVal)) valMap.put(curVal, curKey);
            }

            return true;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.wordPattern("abba", "dog dog dog dog"));
    }

    public static void main(String[] args) throws Exception {
        LC290 an = new LC290();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
